<#-- @ftlvariable name="broadcasts" type="java.util.Collection<org.spamforces.web.page.BroadcastPage.BroadcastForView>" -->
<#import "macros/common.ftl" as common>

<@common.page>
<span class="error"></span>
<a href="#" class="createBroadcast" broadcastId="0" style="float:right;">{{Create New Broadcast}}</a>
<table class="grid tablesorter" style="margin-left:2em">
    <caption class="caption">{{Broadcasts}}</caption>
    <thead>
    <tr>
        <th width="5%">
            #
        </th>
        <th width="5%">
            parent
        </th>
        <th width="10%">
            {{Status}}
        </th>
        <th width="15%">
            {{User}}
        </th>           
        <th width="35%">
            {{Subject}}
        </th>
        <th width="20%">
            {{Recipient providers}}
        </th>
        <th width="10%">
            {{Action}}
        </th>
    </tr>
    </thead>
    <tbody>
    <#list broadcasts as broadcast>
        <tr>
            <td>
                ${broadcast.fields.id}
            </td>
            <td>
            	<#if broadcast.fields.parentBroadcastId??>
                	${broadcast.fields.parentBroadcastId}
                </#if>
            </td>
            <td style="font-size:90%">
                <div>${broadcast.statusWithTime}</div>
                <div style="margin-top:1em">{{DONE}}: ${broadcast.doneEmailCount} / ${broadcast.doneSmsCount}</div>
                <div>{{QUEUE}}: ${broadcast.queueEmailCount} / ${broadcast.queueSmsCount}</div>
                <div>{{FAILED}}: ${broadcast.failedEmailCount} / ${broadcast.failedSmsCount}</div>
            </td>
            <td>
                ${broadcast.user.email}
            </td>
            <td>
                ${broadcast.fields.subjectTemplate}
            </td>
            <td>
                ${broadcast.emailProvidersList}
            </td>
            <td>
                <a class="editBroadcast" href="${editBroadcast}?id=${broadcast.fields.id}">{{Edit}}</a>
                <a class="previewBroadcast" broadcastId="${broadcast.fields.id}" href="#">{{Preview}}</a>
                <#if (broadcast.canBeDeleted)>
                	<a class="deleteBroadcast" broadcastId="${broadcast.fields.id}" href="#">{{Delete}}</a>
                </#if>
                <a class="createBroadcast" broadcastId="${broadcast.fields.id}" href="#">{{Copy}}</a>
            </td>
        </tr>
    </#list>
    <#if broadcasts?size == 0>
        <tr>
            <td colspan="7">
                {{No broadcasts}}
            </td>
        </tr>
    </#if>
    </tbody>
</table>

<div class="preview" style="width:100%;height:100%;display:none">
	<div style="text-align:center">{{Subject}}</div> 
	<pre class="subjectPre" style="margin:0.5em;width:90%;height:3em; overflow:scroll;">
	</pre>
	<div style="text-align:center">{{Body}}</div>
	<pre class="bodyPre" style="margin:0.5em;width:90%;height:10em; overflow:scroll;">
	</pre>
</div>

<script type="text/javascript">
$("a.previewBroadcast").click(function() {
            var id = $(this).attr("broadcastId");
			$.post("broadcast", {action: "preview", id: id}, function(json)
			{
				if (json["success"] == "true") {
					var popup = window.popup("{{Preview broadcast}}");
					var mydiv = $("div.preview").clone().css("display", "block");
					mydiv.find("pre.subjectPre").html(json["previewSubject"]);
					mydiv.find("pre.bodyPre").html(json["previewBody"]);
					mydiv.appendTo(popup);
				}
			}, "json");
			return false;
        });
$("a.deleteBroadcast").click(function() {
			var id = $(this).attr("broadcastId");
			if (confirm("{{Are you sure you want to delete broadcast number }} '"+id+"' ?"))
        	{    
				$.post("broadcast", {action: "delete", id: id}, function(json)
				{
					if (json["success"] == "true")
						window.location.reload();
					else $("span.error").html(json["error"]);
				}, "json");
			}
			return false;
        });
$("a.createBroadcast").click(function() {
			var id = $(this).attr("broadcastId");
			var msg = (id==0)?"{{Create new broadcast}}?":("{{Create new broadcast based on}} #"+id+"?");
			if (confirm(msg))
        	{    
				$.post("broadcast", {action: "create", id: id}, function(json)
				{
					if (json["success"] == "true")
						window.location = "broadcast?id="+json["id"];
					else $("span.error").html(json["error"]);
				}, "json");
			}
			return false;
        });
</script>
</@common.page>
